﻿using System;
using fastCSharp;
using System.Reflection;

namespace diantou.web.ajax.setFocus
{
    /// <summary>
    /// 关注操作
    /// </summary>
    abstract class focus<callType, focusType> : userHandle<callType>
        where callType : focus<callType, focusType>
        where focusType : diantou.dataModel.focus
    {
        /// <summary>
        /// 设置关注
        /// </summary>
        /// <param name="value"></param>
        /// <param name="onReturn"></param>
        [fastCSharp.code.cSharp.ajax]
        public virtual void Set(focusType value, Action<fastCSharp.net.returnValue<bool>> onReturn)
        {
            value.UserId = currentUser.Id;
            setter(value, onReturn);
        }
        /// <summary>
        /// 删除关注
        /// </summary>
        /// <param name="value"></param>
        /// <param name="onReturn"></param>
        [fastCSharp.code.cSharp.ajax]
        public virtual void Remove(focusType value, Action<fastCSharp.net.returnValue<bool>> onReturn)
        {
            value.UserId = currentUser.Id;
            remover(value, onReturn);
        }

        /// <summary>
        /// 设置关注委托
        /// </summary>
        private static readonly Action<focusType, Action<fastCSharp.net.returnValue<bool>>> setter;
        /// <summary>
        /// 删除关注委托
        /// </summary>
        private static readonly Action<focusType, Action<fastCSharp.net.returnValue<bool>>> remover;
        static focus()
        {
            Type type = typeof(focusType);
            type = type.Assembly.GetType("diantou.dataAccess.tcpCall+" + type.Name);
            setter = (Action<focusType, Action<fastCSharp.net.returnValue<bool>>>)Delegate.CreateDelegate(typeof(Action<focusType, Action<fastCSharp.net.returnValue<bool>>>), type.GetMethod("set", BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(focusType), typeof(Action<fastCSharp.net.returnValue<bool>>) }, null));
            remover = (Action<focusType, Action<fastCSharp.net.returnValue<bool>>>)Delegate.CreateDelegate(typeof(Action<focusType, Action<fastCSharp.net.returnValue<bool>>>), type.GetMethod("remove", BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(focusType), typeof(Action<fastCSharp.net.returnValue<bool>>) }, null));
        }
    }
}
